package ylyhappy.cn.ideavim.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AlertDialog;

import com.google.android.material.textfield.TextInputEditText;

import ylyhappy.cn.ideavim.R;
import ylyhappy.cn.ideavim.lg.db.LineData;

public class ConfigModalFormDialog {
    final ConfigPreviewActivity configPreviewActivity;
    public ConfigModalFormDialog(ConfigPreviewActivity co){
        this.configPreviewActivity = co;
    }

    public AlertDialog buildDialog() {
        // 创建对话框的布局
        LayoutInflater inflater = configPreviewActivity.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.line_dialog, null);

        // 创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(configPreviewActivity);
        builder.setTitle("Editor IdeaVim Single Line")
                .setView(dialogView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // OK
                        String map          = ((TextInputEditText)(dialogView.findViewById(R.id.line_map_text          ))).getText().toString();
                        String aPre         = ((TextInputEditText)(dialogView.findViewById(R.id.line_action_pre_text   ))).getText().toString();
                        String aAfter       = ((TextInputEditText)(dialogView.findViewById(R.id.line_action_after_text ))).getText().toString();
                        String aEn          = ((TextInputEditText)(dialogView.findViewById(R.id.line_action_en_text    ))).getText().toString();
                        String aZh          = ((TextInputEditText)(dialogView.findViewById(R.id.line_action_zh_text    ))).getText().toString();
                        String aShortcut    = ((TextInputEditText)(dialogView.findViewById(R.id.line_shortcut_text     ))).getText().toString();
                        String cr           = ((TextInputEditText)(dialogView.findViewById(R.id.line_cr_text           ))).getText().toString();

                        LineData lineData = new LineData(map, aPre, aAfter, aEn, aZh, aShortcut, cr);


//                        LineData lineData = new LineData(
//                                // NOT OK
//                            lineDialogBinding.lineMapText           .getText().toString(),
////                                t1.getEditText().getText().toString(),
//                            lineDialogBinding.lineActionPreText     .getText().toString(),
//                            lineDialogBinding.lineActionEnText      .getText().toString(),
//                            lineDialogBinding.lineActionZhText      .getText().toString(),
//                            lineDialogBinding.lineShortcutText      .getText().toString(),
//                            lineDialogBinding.lineActionAfterText   .getText().toString(),
//                            lineDialogBinding.lineCrText            .getText().toString()
//                        );
                        Log.d("DIALOG", "onClick: " + lineData);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        // 显示对话框
        AlertDialog dialog = builder.create();
        return dialog;
    }
}
